package com.study.easyexcel.business.controller;

import com.study.easyexcel.business.service.WorkService;
import com.study.easyexcel.common.exception.ExcelImportException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;

/**
 * @author yuhuan
 * @date 2023/5/8
 */
@RestController
@RequestMapping("/easy/excel")
public class WorkGateway {

    final WorkService workService;

    public WorkGateway(WorkService workService) {
        this.workService = workService;
    }

    /**
     * 固定属性导出，导出对象的属性映射到导出的列名
     */
    @GetMapping("/export")
    public void exportExcel(HttpServletResponse response) {
        workService.exportExcel(response);
    }

    /**
     * 固定属性的模板导出，导出多个工作溥，包括无表头的工作溥
     */
    @GetMapping("/temp/export")
    public void exportTemplate(HttpServletResponse response) {
        workService.exportTemplate(response);
    }

    /**
     * 导出时间轴，导出的时间映射到导出的列名（周期内的每日时间作为列名）
     */
    @GetMapping("/day/axis/export")
    public void exportDayAxis(HttpServletResponse response, String startTime, String endTime) {
        Date start = Date.from(LocalDateTime.parse(startTime).toInstant(ZoneOffset.UTC));
        Date end = Date.from(LocalDateTime.parse(endTime).toInstant(ZoneOffset.UTC));
        workService.exportDayAxis(response, start, end);
    }

    /**
     * 可变属性的模板导出，动态的列表映射到导出的列名
     */
    @GetMapping("/variable/temp")
    public void variableTemplate(HttpServletResponse response) {
        workService.variableTemplate(response);
    }

    /**
     * 分身式填充模板
     */
    @GetMapping("/fill/share")
    public void shareFillExport(HttpServletResponse response) {
        workService.shareFillExport(response);
    }

    /**
     * 组合式填充模板
     */
    @GetMapping("/fill/merge")
    public void mergeFillExport(HttpServletResponse response) {
        workService.mergeFillExport(response);
    }

    /**
     * 合成式填充模板
     */
    @GetMapping("/fill/synthetic")
    public void syntheticFillExport(HttpServletResponse response) {
        workService.syntheticFillExport(response);
    }

    /**
     * 从文件对象导入固定属性
     */
    @PostMapping("/import")
    public String importExcel(MultipartFile file) {
        try {
            workService.importExcel(file);
        } catch (ExcelImportException e) {
            return e.getMessage();
        }
        return "OK";
    }

    /**
     * 从文件对象导入可变属性（案例使用了事务）
     */
    @GetMapping("/variable/import")
    public String variableImport(MultipartFile file) {
        try {
            workService.variableImport(file);
        } catch (ExcelImportException e) {
            return e.getMessage();
        }
        return "OK";
    }

    /**
     * 从本地文件导入固定属性（案例使用了事务）
     */
    @GetMapping("/local/import")
    public String importLocalExcel() {
        try {
            workService.importLocalExcel();
        } catch (ExcelImportException e) {
            return e.getMessage();
        }
        return "OK";
    }
}
